<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--岗位数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="岗位名称" prop="userName">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入岗位名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="primary" round icon="el-icon-user-solid" size="mini"
                       @click="dataTableVisiable=false">已选（{{selectData.length}}）</el-button>
          </el-form-item>
        </el-form>
        <el-table v-show="dataTableVisiable==true" ref="dataTable"
                  :data="staionList"
                  :row-key="getRowKeys"
                  :reserve-selection="true"
                  @select="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="岗位名称"  prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="岗位编号"  prop="id"   />
        </el-table>

        <pagination v-if="dataTableVisiable==true"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pagesize"
                    :page-sizes="[5,10,20]"
                    @pagination="getList"
        />

        <el-table v-show="dataTableVisiable==false" :data="selectData" style="">
          <el-table-column type="index" width="50" label="序号"></el-table-column>
          <el-table-column prop="name" label="岗位名称"></el-table-column>
          <el-table-column prop="id" label="岗位编号" ></el-table-column>
        </el-table>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {positionListPage} from "@/api/orgm/orgm";
export default {
  name: "byPositions",
  components: { Pagination },
  props:['selectData','ccData'],
  data(){
    return{
      staionList:this.ccData,
      total:0,
      dataTableVisiable:true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        name:'',
        page: 1,
        pagesize: 5
      },
    }
  },
  mounted() {
    this.getList();
  },
  methods:{
    /** 查询用户列表 */
    async getList() {
      if(this.staionList.length<=0) {
        const res = await positionListPage(this.queryParams);
        if (res.error === 200) {
          this.staionList = res.result.list;
          this.total = res.result.total;
          this.SelectionChange(this.selectData);
        } else {
          this.$message.error(`获取列表失败`);
        }
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dataTableVisiable=true;
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    /** 选中/取消选中 **/
    handleSelectionChange(row,unChecked) {
      const rows = this.selectData;
      let selected = row.length && row.indexOf(unChecked) !== -1;
      if(selected==false||row.length==0){
        this.selectData=rows.filter(r=>{
          if(r.id!=unChecked.id)
            return r;
        })
      }
      else {
        if (this.selectData.length > 0) {
          if (row.length > 0) {
            row.forEach(r => {
              let isHas = false;
              rows.forEach(s => {
                if (s.id == r.id) {
                  isHas = true;
                }
              })
              if (isHas == false) {
                this.selectData.push(r);
              }
            })
          }
        } else {
          this.selectData = row;
        }
      }
    },
    /**默认选中**/
    SelectionChange(rowData) {
      this.$nextTick(() => {
        let table = this.staionList;
        this.selectData=rowData;
        table.forEach(row => {
          rowData.forEach(selectRow=>{
            if(row.id==selectRow.id){
              this.$refs.dataTable.toggleRowSelection(row, true);
            }
          });
        });
      });
    },
    getRowKeys(row) {
      return row.id
    },
    /** 保存方法 **/
    handlerSave(){
      return this.selectData;
    }
  }
}
</script>

<style scoped>

</style>
